package me.firstapp.config;

import java.io.IOException;
import java.util.Properties;

import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;

import me.firstapp.spi.bbs.AdvertisementService;
import me.firstapp.spi.bbs.CollectionService;
import me.firstapp.spi.bbs.EmailService;
import me.firstapp.spi.bbs.MemberService;
import me.firstapp.spi.bbs.ReplyService;
import me.firstapp.spi.bbs.SectionService;
import me.firstapp.spi.bbs.TopicService;
import me.firstapp.spi.shop.CategoryService;
import me.firstapp.spi.shop.OrderService;
import me.firstapp.spi.shop.PointRechargeCardService;
import me.firstapp.spi.shop.PointService;
import me.firstapp.spi.shop.ProductService;
import me.firstapp.spi.shop.ProductSkuRelService;
import me.firstapp.spi.shop.SkuService;
import me.firstapp.spi.system.ApiClientService;
import me.firstapp.spi.system.EmailConfigService;
import me.firstapp.spi.system.OperatorService;
import me.firstapp.spi.system.StatisticsService;
import me.firstapp.spi.zerone.CqsscService;

@Configuration
public class RootConfig {

	@Bean
	public PropertiesFactoryBean configProperties() {
		PropertiesFactoryBean configProperties = new PropertiesFactoryBean();
		configProperties.setLocations(new ClassPathResource("config.properties"));
		return configProperties;
	}

	@Bean
	public HttpInvokerProxyFactoryBean memberService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/member.service");
			proxy.setServiceInterface(MemberService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean sectionService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/section.service");
			proxy.setServiceInterface(SectionService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean topicService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/topic.service");
			proxy.setServiceInterface(TopicService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean productService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/product.service");
			proxy.setServiceInterface(ProductService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean skuService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/sku.service");
			proxy.setServiceInterface(SkuService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean orderService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/order.service");
			proxy.setServiceInterface(OrderService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean pointRechargeCardService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/pointRechargeCard.service");
			proxy.setServiceInterface(PointRechargeCardService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean collectionService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/collection.service");
			proxy.setServiceInterface(CollectionService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean emailService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/email.service");
			proxy.setServiceInterface(EmailService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean replyService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/reply.service");
			proxy.setServiceInterface(ReplyService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean categoryService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/category.service");
			proxy.setServiceInterface(CategoryService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean pointService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/point.service");
			proxy.setServiceInterface(PointService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean productSkuRelService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/productSkuRel.service");
			proxy.setServiceInterface(ProductSkuRelService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean advertisementService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/advertisement.service");
			proxy.setServiceInterface(AdvertisementService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean apiClientService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/apiClient.service");
			proxy.setServiceInterface(ApiClientService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean emailConfigService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/emailConfig.service");
			proxy.setServiceInterface(EmailConfigService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean operatorService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/operator.service");
			proxy.setServiceInterface(OperatorService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean statisticsService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/statistics.service");
			proxy.setServiceInterface(StatisticsService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean cqsscService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/cqssc.service");
			proxy.setServiceInterface(CqsscService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}
}
